package vn.bk.hotel;

import vn.bk.hotel.dialog.ChooseCity;
import vn.bk.hotel.dialog.ChooseLandmarks;
import vn.bk.hotel.dialog.ChooseTypeHotelLandmark;

import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gms.internal.ay;

import android.content.Intent;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class SearchByLandmark extends SherlockFragment implements
	OnClickListener {
    private static ImageView addRate, minusRate;
    private static TextView textNumRate;
    private static int numRate = 0;
    private static boolean starEnabled = true;

    private Button search;
    private static Button landMarks, chooseCity;
    private static Button typeLandMarks;
    private static String[] type, cityLandmark;
    private static String[] allUriType;
    private static String myUri;

    private static int positionRadius = 0;
    private static int positionCity = 0;
    static LinearLayout layoutRate;

    // private static LinearLayout layoutRank;

    /**
     * The Fragment's UI is just a simple text view showing its instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.tab_landmark, container, false);
	addRate = (ImageView) v.findViewById(R.id.img_increase_rate_land);
	minusRate = (ImageView) v.findViewById(R.id.img_decrease_rate_land);
	textNumRate = (TextView) v.findViewById(R.id.tv_num_rate_land);
	layoutRate =(LinearLayout) v.findViewById(R.id.layout_rank_landmark);

	addRate.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		if (numRate < 5 && starEnabled) {
		    numRate++;
		    textNumRate.setText(String.valueOf(numRate));
		}
	    }
	});
	minusRate.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		// TODO Auto-generated method stub
		if (numRate > 0 && starEnabled) {
		    numRate--;
		    textNumRate.setText(numRate == 0 ? "All" : String
			    .valueOf(numRate));
		}
	    }
	});

	chooseCity = (Button) v.findViewById(R.id.choose_city);

	landMarks = (Button) v.findViewById(R.id.spin_landmark);
	typeLandMarks = (Button) v.findViewById(R.id.type_landmark_spinner);
	search = (Button) v.findViewById(R.id.bt_search_landmark);

	cityLandmark = getResources().getStringArray(R.array.city_landmark);
	type = getResources().getStringArray(R.array.type);
	allUriType = getResources().getStringArray(R.array.type_uri);
	myUri = allUriType[0];
	// layoutRank = (LinearLayout)
	// v.findViewById(R.id.layout_rank_landmark);

	init();
	setAction();

	return v;
    }

    private void init() {

	changeTypeHotel(0);
	
	changeCity(0,
		getResources().getStringArray(R.array.landmark_hn_text)[0]);

    }

    private static void disableNumRate() {
	addRate.setImageResource(R.drawable.up_disable);
	minusRate.setImageResource(R.drawable.down_disable);
	
	layoutRate.setVisibility(LinearLayout.GONE);
    }

    private static void enableNumRate() {
	addRate.setImageResource(R.drawable.up_normal);
	minusRate.setImageResource(R.drawable.down_normal);
	layoutRate.setVisibility(LinearLayout.VISIBLE);
    }

    private void setAction() {

	search.setOnClickListener(this);

	chooseCity.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		// TODO Auto-generated method stub
		ChooseCity dialog = new ChooseCity(getActivity());
		dialog.show();
	    }
	});

	landMarks.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		// TODO Auto-generated method stub
		ChooseLandmarks dialog = new ChooseLandmarks(getActivity(),
			positionCity);
		dialog.show();
	    }
	});
	typeLandMarks.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		// TODO Auto-generated method stub
		ChooseTypeHotelLandmark dialog = new ChooseTypeHotelLandmark(
			getActivity());
		dialog.show();
	    }
	});

    }

    public static void changeCity(int position, String text) {
	positionCity = position;
	positionRadius = 0;
	landMarks.setText(text);
	chooseCity.setText(cityLandmark[position]);
    }

    public static void changeTypeHotel(int position) {
	myUri = allUriType[position];
	typeLandMarks.setText(type[position]);
	if (position == 1) {
	    starEnabled = true;
	    textNumRate.setText(numRate == 0 ? "All" : String.valueOf(numRate));
	} else {
	    starEnabled = false;
	    textNumRate.setText("All");
	}
	if (starEnabled) {
	    enableNumRate();
	} else {
	    disableNumRate();
	}
    }

    public static void changeLandmark(int paramInt, String paramString) {
	positionRadius = paramInt;
	landMarks.setText(paramString);
    }

    @Override
    public void onClick(View v) {
	// TODO Auto-generated method stub
	switch (v.getId()) {

	case R.id.bt_search_landmark:
	    ListHotel.typeSearch = ListHotel.SEARCH_BY_LANDMARK;
	    ListHotel.numberStar = numRate;
	    ListHotel.keyWord = "";
	    String data = null;
	    switch (positionCity) {
	    case 0:
		data = getResources().getStringArray(
			R.array.landmark_hn_location)[positionRadius];
		break;
	    case 1:
		data = getResources().getStringArray(
			R.array.landmark_hcm_location)[positionRadius];
		break;
	    case 2:
		data = getResources().getStringArray(
			R.array.landmark_dn_location)[positionRadius];
		break;
	    case 3:
		data = getResources().getStringArray(
			R.array.landmark_hp_location)[positionRadius];
		break;
	    default:
		data = getResources().getStringArray(
			R.array.landmark_hn_location)[positionRadius];
		break;
	    }
	    String[] splitData = data.split(";");
	    ListHotel.uriType = myUri;
	    ListHotel.latitude = Double.parseDouble(splitData[0]);
	    ListHotel.longitude = Double.parseDouble(splitData[1]);
	    ListHotel.radius = Float.parseFloat(splitData[2]);

	    getActivity().startActivity(
		    new Intent(getActivity(), ListHotel.class));
	    break;
	}
    }

}